﻿using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Controls;
using System.Data;
using System.Linq;
using MediaData;

namespace SharpMedia.Pages
{
    public class HarddiskConfigPage : MediaPage
    {
        HUDMoveList list;
        HUDImage selection;
        public HarddiskConfigPage(HUDPage parent) : base(parent)
        {
            Add(new HUDImage("white.png") 
            { 
                Alpha = 160, 
                Anchor = Anchor.All, 
                Bounds = this.Bounds, 
                ConstrainProportions = false, 
                Color = ColorS.Black 
            });
            list = new HUDMoveList(new SizeS(400, 70), false) { Size = new SizeS(400, 560) };



            Add(new HUDCentering(this, new HUDImage("box.png") { ConstrainProportions = false, Size = new SizeS(440, 600) }));
            selection = new HUDImage("selection.png") { Size = new SizeS(400, 64) };
            Add(selection);
            Add(new HUDCentering(this, list));

            lock (SharpMedia.UpdateLockObject)
                if (!SharpMedia.UpdateLock)
                    FillList();

            selection.Location = (list.Selection + list.AbsoluteBounds.Location).Location;
        }

        public void FillList()
        {
            list.Clear();

            var volumes = (from f in SharpMedia.Database.Data.Files
                          select f.Volume).Distinct();


            
            foreach (var s in volumes)
            {
                HarddiskItem itamu = new HarddiskItem() { Volume = s };
                list.Add(itamu);
                itamu.Icon.Color = SharpMedia.RootManager.IsOnline(itamu.Volume) ? ColorS.White : ColorS.Red;
            }

            list.MoveItems(false);

            selection.Location = (list.Selection + list.AbsoluteBounds.Location).Location;

        }

        public override void ResizeMove(RectangleS oldRect, RectangleS newRect)
        {
            base.ResizeMove(oldRect, newRect);
            if(selection != null)
                selection.Location = (list.Selection + list.AbsoluteBounds.Location).Location;
        }

        protected override void MediaChanged(MediaDataSet changes)
        {
            lock (SharpMedia.UpdateLockObject)
                if (!SharpMedia.UpdateLock)
                    FillList();
        }

        public override void Show() { Fade(255, 6f); }
        public override void Close() { Fade(0, 6f); }
        
        public override void Command(Lizk.SimpleHUD.HUDCommands command, object tag)
        {
            switch (command)
            {
                case HUDCommands.Up:
                    list.SelectedIndex--;
                    selection.Destination = list.Selection.Location + list.AbsoluteBounds.Location;
                    break;
                case HUDCommands.Down:
                    list.SelectedIndex++;
                    selection.Destination = list.Selection.Location + list.AbsoluteBounds.Location;
                    break;
                case HUDCommands.Left:
                    break;
                case HUDCommands.Right:
                    break;
                case HUDCommands.PageUp:
                    break;
                case HUDCommands.PageDown:
                    break;
                case HUDCommands.Select:
                    SharpMedia.WaitForLock();

                    lock (SharpMedia.Database.Data.LockObject)
                    {
                        // TODO fix!

                        var files = from f in SharpMedia.Database.Data.Files
                                    where f.Volume == list.GetSelectedControl<HarddiskItem>().Volume
                                    select f;

                        foreach (var f in files)
                        {
                            f.Delete();
                        }


                        //VolumeScanner.RemoveUnusedTitles(SharpMedia.Database.Data);
                    }
                    
                    SharpMedia.WorkDone();
                    break;
                case HUDCommands.Back:
                    SharpMedia.Back();
                    break;
                case HUDCommands.Menu:
                    break;
                case HUDCommands.Fullscreen:
                    break;
                case HUDCommands.UpdateDatabase:
                    break;
                case HUDCommands.Web:
                    break;
                case HUDCommands.Preset1:
                    break;
                case HUDCommands.Preset2:
                    break;
                case HUDCommands.Preset3:
                    break;
                case HUDCommands.Play:
                    break;
                case HUDCommands.Pause:
                    break;
                case HUDCommands.PlayPause:
                    break;
                case HUDCommands.Stop:
                    break;
                case HUDCommands.Forward:
                    break;
                case HUDCommands.Rewind:
                    break;
                case HUDCommands.Next:
                    break;
                case HUDCommands.Previous:
                    break;
                case HUDCommands.KeyPress:
                    break;
                default:
                    break;
            }
        }

        public class HarddiskItem : HUDContainerSimple
        {
            private HUDLabel label;
            public HUDImage Icon { get; private set; }

            public string Volume 
            {
                get { return label.Text; }
                set { label.Text = value; }
            }
            public HarddiskItem()
            {
                Bounds = new RectangleS(0, 0, 400, 70);
                Add(label = new HUDLabel("", "tahoma23b") { Bounds = new RectangleS(70, 3, 360, 64), Alignment = Alignment.Left | Alignment.VCenter });
                Add(Icon = new HUDImage("harddisk.png") { Bounds = new RectangleS(0, 3, 64, 64) });
            }
        }
    
    }
}
